# -*- coding: utf-8 -*-
from PyQt4 import QtCore , QtGui
from assetMaker_UI import assetMaker_UI
from lib.papyDB import papyDB ,tableInfo
from lib.papyDBTable import *
from lib import globalVar as GV
import os
try :
    import maya.cmds as cmds
    import sys
    sys.path.append('C:\Python26\Lib\site-packages')
    sys.path.append('D:\kali\General\papyrus\py')
    import MySQLdb
    MAYA = 1
except :
    MAYA = 0



class cAssetMaker( assetMaker_UI ):
    def __init__(self):
        assetMaker_UI.__init__(self)
        self.setUI()
        self.makeAsset_btn.clicked.connect(self.regAsset)    

    def regAsset(self):
        prjCodeName = str( self.prj_cb.currentText() )       
        seqID = False
        shotID = False
                
        prjID = papyDB.getID("PROJECT", "prjID", "prjCodeName", prjCodeName  )
        if str( self.seq_cb.currentText() ) != '--Master--':
            seqID = SEQUENCES.search('seqID' , seqName=str( self.seq_cb.currentText() ))
        if str( self.shot_cb.currentText() ) != '--Global--':
            shotID = SHOT.search('shotID' , shotName=str( self.shot_cb.currentText() ) , seqID=seqID)
            
        assetType = str( self.assetType_CBox.currentText() )
        assetName = str( self.assetName_line.text() )
        assetTags = str( self.assetTag_line.text() ).split(',') if str( self.assetTag_line.text() ) !='' else []
        assetDesc = str(self.desc_txtEdt.toPlainText() )        

        if assetName=='':
            self.alertWin("Error" , "Please insert Asset Name.")
            return
                
        papyDB.regTable('ASSET' , assetName , 1 , '0000-00-00' , '0000-00-00' , assetType , '0000-00-00' , assetDesc, 'NULL')
        lastAssetID= papyDB.cr.lastrowid
        
        # Tagging        
        if len(assetTags) > 0 :            
            for x in assetTags:
                if not x in [k[0] for k in  papyDB.getFetch('select Tag from ASSET_TAG')] :
                    papyDB.regTable('ASSET_TAG' , x ) 
#                    lastTagID = papyDB.cr.lastrowid
                tagID = papyDB.getID('ASSET_TAG', 'assetTagID', 'Tag', x )                
                papyDB.regTable('ASSET_has_ASSET_TAG' , lastAssetID , tagID )
#        print prjID , seqID , shotID
        if prjID != False and seqID != False and shotID != False :
            papyDB.regTable('SHOT_has_ASSET' , shotID , lastAssetID )  
        elif prjID != False and seqID != False and shotID == False :
            papyDB.regTable('SEQUENCES_has_ASSET' , seqID , lastAssetID )
        else :
            papyDB.regTable('PROJECT_has_ASSET' ,  prjID , lastAssetID  )
                        
        ########################################################################################################################### MAKE FILE !
#        filePath = os.path.join(GV.PROJECT_SERVER , prjCodeName , "Dev", "Model" , assetName+'_001.ma')
#        tempFile = GV.tempMAYAfile
#        print tempFile, filePath
#        os.system("copy %s %s" % (tempFile , filePath) )    
#        if MAYA :
#            cmds.file( tempFile , o=True , f=True )
#            cmds.file( rn=filePath )
#            cmds.file( s=True , f=True )
                      
        self.alertWin("Notice" , "Successfully Registered!")
        
    def closeEvent( self , event ) :        
        papyDB.cr.close()



#if __name__ == '__main__':
#    import sys
#    app = QtGui.QApplication(sys.argv)
#    cAssetMaker = cAssetMaker()
#    app.exec_()
    
def theTool( ):
    if MAYA == True :
        global app    
        try:        
            app.close()        
        except :        
            app = cAssetMaker()
        app.show()
    else :
        import sys
        app = QtGui.QApplication(sys.argv)
        mainWin = cAssetMaker()   
        mainWin.show()
        sys.exit(app.exec_())

if __name__ == '__main__':
    theTool()    
    
    